import Classify_list from "../../models/classify_list";
import Classify_types from "../../models/classify_types";
import Read_pare360_data from "../../initData/init_data";

class Parse360 extends Read_pare360_data {
  constructor() {
    super();
    this.get_types_data = this.get_types_data.bind(this);
    this.category_type = this.category_type.bind(this);
    this.category_list = this.category_list.bind(this);
    this.suggest = this.suggest.bind(this);
    this.hotquery = this.hotquery.bind(this);
    this.search = this.search.bind(this);
    this.info = this.info.bind(this);
    this.about = this.about.bind(this);
    this.info_about = this.info_about.bind(this);
    this.parselz = this.parselz.bind(this);
    this.test = this.test.bind(this);
  }

  /**
   * 获取首页列表
   * @query.type ['main', 'serial', 'film', 'variety', 'cartoon', all]
   */
  async get_types_data(req, res, next) {
    const content = {
      msg: '请求成功',
      status: 200,
    }

    if (this.classify_type.map(el => el.key).includes(req.query.type)) {
      content.content = await Classify_list.findOne({classify: req.query.type}) || {};
    } else {
      const list = await Classify_list.find({}) || [];
      content.list = [];
      this.classify_type.map(el => {
        list.some(item => {
          if (item.classify === el.key) {
            content.list.push(item)
            return true;
          }
        })
      });
    }

    res.send(content);
  }

  /**
   * 搜索提示列表
   */
  async suggest(req, res, next) {
    const json = await this.fetch('/suggest', {kw: req.query.kw})
    const body = {
      status: 200,
      msg: '请求成功',
      list: [],
    }

    try {
      if (json.status === 200 && json.data) {
        body.list = json.data;
      } else {
        body.status = 0
        body.msg = json.msg || '失败'
      }
    } catch (e) {
      body.status = 0
      body.msg = json.msg || '失败'
    }

    res.send(body)
  }

  /**
   * 搜索热词
   * @returns {Promise<void>}
   */
  async hotquery(req, res, next) {
    const __jsonp0__ = e => e;
    const jsonp = await this.fetch('/hotquery', {}, {}, 'https://video.so.com/')
    const body = {
      status: 200,
      msg: '请求成功',
      list: [],
    }

    try {
      body.list = eval(jsonp)
      // throw new Error('未找到当前管理员')
    } catch (e) {
      body.msg = '失败'
      body.status = 0
    }

    res.send(body)
  }

  /**
   * 搜索列表
   * @query {kw} string
   * @returns {Promise<void>}
   */
  async search(req, res, next) {
    const json = await this.fetch('/search', {kw: req.query.kw})
    const body = {
      status: 200,
      msg: '请求成功',
      list: [],
    }

    try {
      if (json.status === 200) {
        body.msg = json.msg;
        if(eval(req.query.is_info) === true) {
          for(let i = 0, len = json.data.length; i < len; i++) {
            let info = await this.fetch('/info', Object.assign({}, {pk: 'com.tv.pro.vip.android'}, ({url: json.data[i].url} || {})));
            if (typeof info.data === 'object') {
              body.list.push(Object.assign({}, json.data[i], info))
            } else {
              info = this.decrypt(info.data);
              body.list.push(Object.assign({}, json.data[i], info, info.info))
            }
          }
        } else {
          body.list = json.data;
        }
      } else {
        body.msg = '失败'
        body.status = 0
      }
    } catch (e) {
      body.msg = '失败'
      body.status = 0
    }

    res.send(body)
  }

  /**
   * 全部类别
   * @returns {Promise<void>}
   */
  async category_type(req, res) {
    const body = {
      status: 200,
      msg: '请求成功',
    }
    if (this.classify_type.filter(el => el.is_children).map(el => el.key).includes(req.query.category)) {
      body.data = await Classify_types.findOne({classify: req.query.category}) || {}
    } else {
      body.types = await Classify_types.find({}) || []
    }
    res.send(body)
  }

  /**
   * 全部类别列表
   * @returns {Promise<void>}
   */
  async category_list(req, res) {
    const body = {
      status: 200,
      msg: '请求成功',
    }
    const json = await this.fetch('/list', req.query)
    Object.assign(body, json, body)
    res.send(body)
  }


  /**
   * 视频详情（播放）
   * @returns {Promise<void>}
   */
  async info(req, res) {
    const body = {
      status: 200,
      msg: '请求成功',
    }
    const json = await this.fetch('/info', Object.assign({}, {pk: 'com.tv.pro.vip.android'}, (req.query || {})))
    try {
      if (typeof json.data === 'object') {
        res.send(Object.assign({}, body, json))
      } else {
        res.send(Object.assign({}, body, this.decrypt(json.data),))
      }
    } catch (e) {
      res.send({status: 0, msg: body.msg || '失败'})
    }
  }


  /**
   * 相关视频列表（播放）
   * @returns {Promise<void>}
   */
  async info_about(req, res) {
    const body = {
      status: 200,
      msg: '请求成功',
    }
    const json = await this.fetch('/infoAbout', req.query)
    Object.assign(body, body, json)
    res.send(body)
  }


  /**
   * 相关视频、花絮、游戏播放（播放）
   * @returns {Promise<void>}
   */
  async about(req, res) {

    const body = {
      status: 200,
      msg: '请求成功',
    }
    const json = await this.fetch('/about', req.query)
    try {
      if (json.status === 200 && json.data.about) {
        body.about = json.data.about
      }
    } catch (e) {
      body.status = 0;
      body.msg = '失败'
    }
    res.send(body)
  }


  /**
   * 解析地址
   * @returns {Promise<void>}
   */
  async parselz(req, res) {
    const json = await this.fetch('parse/play/', req.query, {}, this.baseURL)
    res.send({
      status: 200,
      msg: '请求成功',
      json: json,
    })
  }


  /**
   * 解析地址
   * @returns {Promise<void>}
   */
  async test(req, res) {
    const json = await this.fetch(req.query.route, req.query, {}, req.query.baseURL || this.baseURL)
    res.send({
      status: 200,
      msg: '请求成功',
      json,
    })
  }

}

export default Parse360;
